function obj_eval = obj_function(param_unc,fp)
tic
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%;
%To constraint the structural parameters;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%;

%Technology (Cobb-Douglas, PS=1);
param_con(1) = exp(param_unc(1)/100);    %Children's skills (alpha1);
param_con(2) = exp(param_unc(2)/100);    %Peers' Skills (alpha2);
param_con(3) = exp(param_unc(3)/100);    %Investments (alpha3);


param_con(4) = param_unc(4)/100;         %Parenting Style (alpha0);

%Technology (CES, PS=0);
param_con(5) = exp(param_unc(5)/100)./(1 + exp(param_unc(5)/100) );         %CES Elasticity (   Parents vs Peers, inner)
param_con(6) = exp(param_unc(6)/100)./(1 + exp(param_unc(6)/100) );         %CES Share Skills ;
param_con(7) = exp(param_unc(7)/100)./(1 + exp(param_unc(7)/100) );         %CES Share Peers;
param_con(8) = -exp(param_unc(8)/100) ;                                     %CES Elasticity ( Skills vs Parents-Peers, outer);
param_con(9) = param_unc(9)/100 ;                                           %Return to Scale CES;

param_con(10) = param_unc(10)/100;           %TFP constant;
param_con(11) = param_unc(11)/100;           %TFP trend;

ind  = 11 ;

%Parent's Preferences;

param_con(ind+1) =      exp(param_unc(ind+1)/100);                    %Weight on Children's Skills;
param_con(ind+2) =      param_unc(ind+2)/100 ;                        %Disutility of PS;
param_con(ind+3) =      param_unc(ind+3)/100 ;                        %Heterogeneity in PS;

ind  = ind + 3 ;

%Child's Preferences;

param_con(ind+1) =  param_unc(ind+1)/100;                       %Constant (gamma0);
param_con(ind+2) =  param_unc(ind+2)/100;                       %Own Skills (gamma1);
param_con(ind+3) =  param_unc(ind+3)/100;                       %Child j 's Skills (gamma2);
param_con(ind+4) =  param_unc(ind+4)/100;                       %Homophily (gamm3);
param_con(ind+5) =  param_unc(ind+5)/100;                       %PS effect (gamma4);


param_con(ind+6) =      param_unc(ind+6)/100 ;                        %Disutility of PS (by Neighborhood Income);

%New Parameters that depend on Mother's Education;
param_con(ind+7) =      param_unc(ind+7)/100 ;                        %Disutility of Ps if College Degree;
param_con(ind+8) =     -param_unc(ind+8)/100 ;                        %TFP if College Degree;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simulate latent variables from latent parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
               
sim = sim_data_function(param_con,fp) ;


simul_data = sim.sim_data ; 
sim_data_network = sim.sim_data_network;

toc

if fp.do_estimation==1

sim_moments = mom_function(simul_data,sim_data_network,fp);
    
diff_mom = fp.data_mom - sim_moments ;

diff_mom_sq = (diff_mom.^(2));

gmm = sum(diff_mom_sq);

obj_eval = gmm;


display('Current Estimates for Parameters (Unconstrained)')
param_unc(:)'./100
display('Current Estimates for Parameters (constrained)')
param_con(:)'

else
 
sim_moments = mom_function(simul_data,sim_data_network,fp);    
obj_eval.sim_moments = sim_moments;    

Value0     =    sim.Value0;
Value1     =    sim.Value1;

obj_eval.Value0= Value0;
obj_eval.Value1= Value1;


obj_eval.structural_params = param_con;    
obj_eval.simul_data= simul_data;
obj_eval.sim_data_network=sim_data_network;


end



